package xjtu.se.articlecommunity.buser.usermanage.dao;

import org.apache.ibatis.annotations.*;
import xjtu.se.articlecommunity.buser.usermanage.dto.User;
import xjtu.se.articlecommunity.buser.usermanage.dto.UserManageDto;
import xjtu.se.articlecommunity.buser.usermanage.dto.UserTab;

import java.util.List;

@Mapper
public interface UserManageDao {
    List<User> queryUserList(UserManageDto dto);

    @Select("select count(*) from t_user where u_id=#{u_id}")
    int findUserByID(String uid);

    @Select("select count(*) from t_user_tab where ut_id=#{ut_id}")
    int findUserTabByID(String ut_id);

    @Select("select ut_name from t_user_tab where ut_name=#{ut_name}")
    String findTabByName(String ut_name);

    @Delete("delete from user_tab where ut_id=#{ut_id}")
    void dropTabInUT(String ut_id);

    @Delete("delete from t_user_tab where ut_id=#{ut_id}")
    void delTab(String ut_id);

    @Insert("insert into user_tab (u_id, ut_id) values (#{u_id}, #{ut_id})")
    void addTabToUser(String u_id, String ut_id);

    @Insert("insert into t_user_tab (ut_id, ut_name) values (#{ut_id}, #{ut_name})")
    void newTab(String ut_id, String ut_name);

    @Delete("delete from user_tab where u_id=#{u_id}")
    void rmTabForUser(String u_id);

    @Update("update t_user set u_status=#{status_code} where u_id=#{u_id}")
    boolean setUserStatus(String u_id, int status_code);

    @Select("select" +
            " tut.ut_id, tut.ut_name," +
            " (select count(*) from user_tab ut where ut.u_id=#{u_id} and ut.ut_id=tut.ut_id ) has" +
            " from t_user_tab tut")
    List<UserTab> queryUserTabsByUID(String u_id);
}
